<?php

///////////////////////////////////////////////////////////////////////////////
// 
// JK_QUOTE
//
// This script gets a TSX quote from Yahoo, formats it, and inserts it
//
///////////////////////////////////////////////////////////////////////////////

// get quote from Yahoo and split
// note: ^GSPTSE is flag for TSX, l1c for outputs
$quote_raw = file_get_contents('http://finance.yahoo.com/d/quotes.csv?s=^GSPTSE&f=l1c');
$quote_raw_split = explode(",", $quote_raw);

// get value of index, formatted as XX XXX.X
$index_raw_split = explode(".", $quote_raw_split[0]);
$index_raw_array = array( substr($index_raw_split[0],0,-3)," ", substr($index_raw_split[0],-3,3), ".", substr($index_raw_split[1],0,1) );
$index = join($index_raw_array);

// get absolute change of index, formatted as XXXX.X
$change_raw = explode(" - ", $quote_raw_split[1]);
$change = substr($change_raw[0], 1, -1);

// determine colour to format change in (positive change = green, negative = red)
$change_class = ((strcasecmp(substr($change,0,1),"+") == 0) ? "HeaderTickerPos" : "HeaderTickerNeg");

// finally, write formatted quote string
$out_array = array( $index, " <span class=\"", $change_class, "\">", $change, "</span>");
echo join($out_array);

?>